﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Silverlight.Navigation.Mapping;

namespace Silverlight.Navigation.Controls
{
    public partial class SubMenu : ChildWindow
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the SubMenu class.
        /// </summary>
        public SubMenu()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the SubMenu class.
        /// </summary>
        /// <param name="subMenuItems">The items associated with the submenu.</param>
        public SubMenu(SubMenuItemCollection subMenuItems)
            : this()
        {
            if (subMenuItems == null)
                throw new ArgumentNullException("subMenuItems");

            this.SubMenuItems = subMenuItems;
        }

        #endregion

        #region Properties

        /// <summary>
        /// The selected sub menu item.
        /// </summary>
        public SubMenuItem SelectedSubMenuItem { get; set; }

        /// <summary>
        /// The items associated with the sub menu.
        /// </summary>
        public SubMenuItemCollection SubMenuItems 
        {
            get
            {
                if (this.SubMenuItemsControl.ItemsSource != null)
                    return (SubMenuItemCollection)this.SubMenuItemsControl.ItemsSource;
                else
                    return null;
            }
            set
            {
                this.SubMenuItemsControl.ItemsSource = value;
            }
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Handles the click event of the button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks>
        /// This method sets the SelectedSubMenuItem property to DataContext of the button that
        /// was clicked and sets the DialogResult of the child window to true.
        /// </remarks>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource is Button)
            {
                Button sourceButton = (Button)e.OriginalSource;

                if (sourceButton.DataContext != null && sourceButton.DataContext is SubMenuItem)
                    this.SelectedSubMenuItem = (SubMenuItem)sourceButton.DataContext;
            }

            this.DialogResult = true;
        }

        #endregion
    }
}

